﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;

using Domzs.Web.Mvc.Captcha;

namespace System.Web.Mvc
{
    public static class HtmlCaptchaHelperExtension
    {
        public static string CaptchaImage(this HtmlHelper helper, int height, int width)
        {
            CaptchaImage image2 = new CaptchaImage();
            image2.Height = height;
            image2.Width = width;
            CaptchaImage image = image2;
            HttpRuntime.Cache.Add(image.UniqueId, image, null, DateTime.Now.AddSeconds(Domzs.Web.Mvc.Captcha.CaptchaImage.CacheTimeOut), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
            StringBuilder builder = new StringBuilder(0x100);
            builder.Append("<input type=\"hidden\" name=\"captcha-guid\" value=\"");
            builder.Append(image.UniqueId);
            builder.Append("\" />");
            builder.AppendLine();
            builder.Append("<img src=\"");
            builder.Append("captcha.ashx?guid=" + image.UniqueId);
            builder.Append("\" alt=\"CAPTCHA\" width=\"");
            builder.Append(width);
            builder.Append("\" height=\"");
            builder.Append(height);
            builder.Append("\" />");
            return builder.ToString();
        }

        public static string CaptchaTextBox(this HtmlHelper helper, string name)
        {
            return string.Format("<input type=\"text\" id=\"{0}\" name=\"{0}\" value=\"\" maxlength=\"{1}\" autocomplete=\"off\" />", name, Domzs.Web.Mvc.Captcha.CaptchaImage.TextLength);
        }
    }
}
